<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Version;
use think\Config;
use think\Db;
use think\Hook;
use think\Request;

/**
 * 公共接口
 */
class Base extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->check();
    }

    //验签
    public function check(){
        $data = $this->request->param('');
        $signature = input('signature', '', 'trim');//签名
        //商户号和秘钥必传
        $result = $this->validate($data,[
            'appKey'=>'require',
            'appSecret'=>'require',
        ],[
            'appKey.require'   => '商户号不能为空',
            'appSecret.require'   => '秘钥不能为空',
        ]);
        if ($result !== true) {
            $this->error($result);
        }
        //查询商户号是否有效
        $customer = Db::name('auth_user')->where(array('appKey'=>$data['appKey'],'appSecret'=>$data['appSecret']))->find();
        if(empty($customer)){
            $this->error(__('商户名或秘钥有误'));
        }
        //是否过期
        if($customer['is_forever']==1&&$customer['auth_time']<time()){
            $this->error(__('商户名或秘钥已过期'));
        }
        //生成签名
       /* ksort($data);//自然排序
        $md5str = "";
        unset($data['signature']);
        foreach ($data as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }
        $sign = strtolower(md5($md5str));
        if($sign != $signature) {
            $this->error(__('验签失败'.$sign));
        }*/
    }
}
